package com.anrongtec.zcpt.view;

import com.anrongtec.zcpt.R;

import android.content.Context;
import android.support.design.widget.TextInputLayout;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 输入框 可以设置输入类型、是否为密码输入、设置最大长度默认开启计数。
 * 
 * @author chenjiaotuo
 *
 */
public class InputView extends LinearLayout {
	/**
	 * 清除输入按钮
	 */
	private ImageView ivClean;
	/**
	 * 显示密码按钮
	 */
	private ImageView ivShowPwd;
	/**
	 * 输入框
	 */
	private EditText etInput;
	private TextInputLayout til;
	/**
	 * 输入类型
	 */
	private int inputType;
	/**
	 * 是否为密码输入框
	 */
	private boolean isShowPwd = false;

	public InputView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);

		LayoutInflater.from(context).inflate(R.layout.input_view, this);
		ivClean = (ImageView) findViewById(R.id.iv_clean_edit);
		ivShowPwd = (ImageView) findViewById(R.id.iv_eye_edit);
		etInput = (EditText) findViewById(R.id.et_edit);
//		etInput.setText("111111");
		til = (TextInputLayout) findViewById(R.id.til_edit);

		OnClickListener listener = new EditViewListener();
		ivClean.setOnClickListener(listener);
		ivShowPwd.setOnClickListener(listener);

		til.getEditText().addTextChangedListener(new EditViewTextWatcher());
	}

	public InputView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public InputView(Context context) {
		this(context, null, 0);
	}

	/**
	 * 获取输入内容
	 * 
	 * @return
	 */
	public String getText() {
		return etInput.getText().toString();
	}
	
	/**
	 * 设置编辑框文字
	 * @param s
	 */
	public void setText(String s) {
		etInput.setText(s);
	}

	/**
	 * 设置提示信息
	 * 
	 * @param hint
	 */
	public void setHint(String hint) {
		til.setHint(hint);
	}

	/**
	 * 设置是否为密码输入框
	 * 
	 * @param flag
	 */
	public void setPwdEdit(boolean flag) {
		ivShowPwd.setVisibility(flag ? View.VISIBLE : View.GONE);
	}

	/**
	 * 设置输入类型
	 * 
	 * @param type
	 */
	public void setInputType(int type) {
		etInput.setInputType(type);
		this.inputType = type;
	}

	/**
	 * 设置内容
	 * @param text 文本
	 * @param index 光标位置
	 */
	public void setText(String text,int index) {
		etInput.setText(text);
		if(!TextUtils.isEmpty(text)){
			etInput.setSelection(index);
		}
	}

	/**
	 * 设置颜色
	 */
	public void setTextColor(int color){
		etInput.setTextColor(color);
	}
	
	/**
	 * 设置输入最大长度
	 * 
	 * @param length
	 */
	public void setMaxLength(int length) {
		til.setCounterEnabled(true);
		til.setCounterMaxLength(length);
		setText(getText(), etInput.getSelectionEnd());
		
	}

	/**
	 * 获取输入框
	 * 
	 * @return
	 */
	public EditText getEditText() {
		return etInput;
	}

	private class EditViewTextWatcher implements TextWatcher {
		private int lastLength = 0;

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			lastLength = s.length();
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		@Override
		public void afterTextChanged(Editable s) {
			// 输入数据之后显示清除按钮
			if (til.getEditText().getText().toString().length() > 0) {
				ivClean.setVisibility(View.VISIBLE);
			} else {
				ivClean.setVisibility(View.GONE);
			}

			if (til.getCounterMaxLength() != -1) {
				// 长度大于最大计数长度报错
				if (til.getEditText().getText().length() > til.getCounterMaxLength()) {
					if (!til.isErrorEnabled()) {
						til.setErrorEnabled(true);
						til.setError("");
					}
					til.setError("长度应为" + til.getCounterMaxLength() + "位数");
				} else {
					if (til.isErrorEnabled()) {
						til.setErrorEnabled(false);
					}
				}
			}
			if (s.length() > lastLength) {
				etInput.setSelection(s.length());
			}
		}
	}

	private class EditViewListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.iv_clean_edit:
				etInput.setText("");
				break;

			case R.id.iv_eye_edit:
				if (isShowPwd) {
					ivShowPwd.setImageResource(R.drawable.eye_close);
					etInput.setInputType(inputType);
					isShowPwd = false;
				} else {
					ivShowPwd.setImageResource(R.drawable.eye_open);
					etInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
					isShowPwd = true;
				}
				etInput.setSelection(etInput.getText().toString().length()); // 光标移动到最后
				break;
			}
		}
	}
}
